#! python3
# 要删除几百CSV文件的第一行

import csv
import os

import csv_demo


def delCSVFirstLine(inPath):
    """
    从 CSV 文件中删除表头:
    遍历目录，将删除表头后的文件保存在新建的headerRemoved子目录下
    :param inPath:
    :return:
    """
    outPath = os.path.join(inPath, 'headerRemoved')  # 文件输出目录
    if not os.path.exists(outPath):
        os.mkdir(outPath)

    inFiles = os.listdir(inPath)
    for filename in inFiles:
        if filename.lower().endswith('.csv'):
            rows = []
            inReader = csv.reader(open(os.path.join(inPath, filename), encoding='utf-8'))
            for row in inReader:
                if inReader.line_num == 1:
                    continue
                else:
                    rows.append(row)

            outFileName = os.path.join(outPath, filename[:len(filename) - 4] + "_new.csv")

            csv_demo.writeCSV(outFileName, 'w', rows)
        else:
            continue


if __name__ == '__main__':
    delCSVFirstLine('file/')
